package com.yupi.ojjudge.judge.codesandbox.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yupi.ojjudge.common.ErrorCode;
import com.yupi.ojjudge.exception.BusinessException;
import com.yupi.ojjudge.judge.codesandbox.CodeSandBox;
import com.yupi.ojjudge.judge.model.ExecuteCodeRequest;
import com.yupi.ojjudge.judge.model.ExecuteCodeResponse;

/**
 * @Author ortonzhu
 * @Date 2024/10/18 19:16
 * 示例沙箱
 */
public class RemoteCodeSandBox implements CodeSandBox {
    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest request) {
        System.out.println("executeCode remote");
        String url = "http://192.168.88.130:8090/executeCode";
        String jsonStr = JSONUtil.toJsonStr(request);
        String responseStr = HttpUtil.createPost(url)
                .body(jsonStr)
                .execute()
                .body();
        if (StrUtil.isBlank(responseStr)) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "execute code error" + responseStr);
        }
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}
